<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class model_fundrising extends CI_Model{
    /**
     * Initialize table and primary field variable
     */
    var $table_product          = "tbl_product";
    var $table_product_image    = "tbl_product_image";
    var $table_tarif            = "tbl_tarif";
    var $table_order            = "tbl_order";
    var $table_order_item       = "tbl_order_item";
    var $table_order_confirm    = "tbl_order_konfirmasi";
    var $primary                = "product_id";
    
    /**
	* Constructor - Sets up the object properties.
	*/
    public function __construct(){
        parent::__construct();
    }
    
    /**
     * Save data of order
     * 
     * @author  Iqbal
     * @param   Array   $data   (Required)  Array data of order
     * @return  Boolean Boolean false on failed process or invalid data, otherwise true
     */
    function save_order($data){
        if( empty($data) ) return false;
        if( $this->db->insert($this->table_order, $data) ) {
            $id = $this->db->insert_id();
            return $id;
        };
        return false;
    }
    
    /**
     * Save data of order item
     * 
     * @author  Iqbal
     * @param   Array   $data   (Required)  Array data of order item
     * @return  Boolean Boolean false on failed process or invalid data, otherwise true
     */
    function save_order_item($data){
        if( empty($data) ) return false;
        if( $this->db->insert($this->table_order_item, $data) ) {
            $id = $this->db->insert_id();
            return $id;
        };
        return false;
    }
    
    /**
     * Save data of order confirm
     * 
     * @author  Iqbal
     * @param   Array   $data   (Required)  Array data of order confirm
     * @return  Boolean Boolean false on failed process or invalid data, otherwise true
     */
    function save_order_confirm($data){
        if( empty($data) ) return false;
        if( $this->db->insert($this->table_order_confirm, $data) ) {
            $id = $this->db->insert_id();
            return $id;
        };
        return false;
    }
    
    /**
     * Retrieve paged list of product 
     * 
     * @author  Iqbal
     * @param   Int     $limit              Limit of paged list         default 0
     * @param   Int     $offset             Offset ot paged list        default 0
     * @param   String  $order_column       Column that make to order   default 'datecreated'
     * @param   String  $order_type         Type of column order        default 'DESC'
     * @return  Object  Result of product paged list
     */
    public function get_all_products_data($limit=0, $offset=0, $conditions='', $order_by=''){
        if( !empty($conditions) ){
            $conditions = str_replace("%product_name%", "product_name", $conditions);
        }
        
        if( !empty($order_by) ){
            $order_by   = str_replace("%datecreated%",  "datecreated",  $order_by);
            $order_by   = str_replace("%product_name%", "product_name", $order_by);
            $order_by   = str_replace("%price%",        "price",        $order_by);
        }
        
        $sql    = '
            SELECT SQL_CALC_FOUND_ROWS A.*, B.path  FROM ' . $this->table_product . ' AS A 
            LEFT JOIN ' . $this->table_product_image . ' AS B 
            ON B.id = A.image ';

        if( !empty($conditions) ){ $sql .= 'WHERE ' . $conditions . ' '; }
        $sql   .= 'ORDER BY '. ($order_by ? $order_by : 'A.datecreated DESC');
        
        if( $limit ) $sql .= ' LIMIT ' . $offset . ', ' . $limit;
        
        $query  = $this->db->query($sql);
        
        if(!$query || !$query->num_rows()) return false;
        
        return $query->result();
    }
    
    /**
     * Get product data by product ID
     * 
     * @author  Iqbal
     * @param   Integer $product_id  (Required)  Product ID
     * @return  Mixed   False on failed process, otherwise object of product.
     */
    function get_productdata($product_id){
        if ( !is_numeric($product_id) ) return false;

        $product_id = absint($product_id);
        if ( !$product_id ) return false;
        
        $query = $this->db->get_where($this->table_product, array($this->primary => $product_id));
        if ( !$query->num_rows() )
            return false;

        foreach ( $query->result() as $row ) {
            $product = $row;
        }
        
        return $product;
    }
    
    /**
     * Get product image by product ID
     * 
     * @author  Iqbal
     * @param   Integer $id  (Required)  Product ID
     * @return  Mixed   False on failed process, otherwise object of product image.
     */
    function get_product_image($id){
        $sql    = '
            SELECT * FROM '.$this->table_product_image.' 
            WHERE '.$this->primary.' = '.$id.'  
            ORDER BY datecreated DESC';
        $qry    = $this->db->query($sql);
        
        if($qry->num_rows > 0){
            return $qry->result();
        }
        return false;
    }
    
    /**
     * Get tarif by term
     * 
     * @author  Iqbal
     * @param   Integer $term  (Required)  Term of tarif
     * @return  Mixed   False on failed process, otherwise object of tarif
     */
    function get_tarif($term){
        $sql    = 'SELECT *, CONCAT_WS( " - ", `kota` , `kecamatan` ) AS term FROM '.$this->table_tarif.' WHERE CONCAT_WS( " - ", `kota` , `kecamatan` ) LIKE "%'.$term.'%"';
        $qry    = $this->db->query($sql);
        
        if($qry->num_rows > 0){
            return $qry->result();
        }
        return false;
    }
    
    /**
     * Retrieve list of tarif 
     * 
     * @author  Iqbal
     * @param   Boolean $single             Option of return dala       default false
     * @param   Int     $limit              Limit of paged list         default 0
     * @param   Int     $offset             Offset ot paged list        default 0
     * @param   String  $order_column       Column that make to order   default 'datecreated'
     * @param   String  $order_type         Type of column order        default 'ASC'
     * @return  Object  Result of tariflist
     */
    public function get_all_tarif_data($single=false,$limit=0, $offset=0, $conditions='', $order_by=''){
        if( !empty($conditions) ){
            $conditions = str_replace("%id_tarif%",     "id_tarif", $conditions);
            $conditions = str_replace("%kota%",         "kota", $conditions);
            $conditions = str_replace("%kecamatan%",    "kecamatan", $conditions);
        }
        
        if( !empty($order_by) ){
            $order_by   = str_replace("%id_tarif%",     "id_tarif",  $order_by);
            $order_by   = str_replace("%kota%",         "kota", $order_by);
            $order_by   = str_replace("%kecamatan%",    "kecamatan",        $order_by);
        }
        
        $sql    = 'SELECT SQL_CALC_FOUND_ROWS * FROM ' . $this->table_tarif . ' ';

        if( !empty($conditions) ){ $sql .= 'WHERE ' . $conditions . ' '; }
        $sql   .= 'ORDER BY '. ($order_by ? $order_by : 'id_tarif ASC');
        
        if( $limit ) $sql .= ' LIMIT ' . $offset . ', ' . $limit;
        
        $query  = $this->db->query($sql);
        
        if( !$query || !$query->num_rows() ) return false;
        
        if( $single ) return $query->row();
        
        return $query->result();
    }
    
    /**
     * Get order data by order ID
     * 
     * @author  Iqbal
     * @param   Integer $order_id  (Required)  Order ID
     * @return  Mixed   False on failed process, otherwise object of order.
     */
    function get_orderdata($order_id){
        if ( !is_numeric($order_id) ) return false;

        $order_id = absint($order_id);
        if ( !$order_id ) return false;
        
        $query = $this->db->get_where($this->table_order, array('order_id' => $order_id));
        if ( !$query->num_rows() )
            return false;

        foreach ( $query->result() as $row ) {
            $order = $row;
        }
        
        return $order;
    }
    
    /**
     * Get order data by conditions
     * 
     * @author  Iqbal
     * @param   String  $field  (Required)  Database field name or special field name defined inside this function
     * @param   String  $value  (Optional)  Value of the field being searched
     * @return  Mixed   Boolean false on failed process, invalid data, or data is not found, otherwise StdClass of order
     */
    function get_order_by($field, $value='')
    {
        $id = '';
        
        switch ($field) {
            case 'id':
                $id     = $value;
                break;
            case 'invoice':
                $id     = '';
                $field  = 'invoice';
                break;
            default:
                return false;
        }
        
        if ( $id != '' && $id > 0 )
            return $this->get_orderdata($id);
        
        if(empty($field)) return false;
        
        $db     = $this->db;
        $query  = $db->get_where($this->table_order, array($field => $value));
        
        if ( !$query->num_rows() )
            return false;

        foreach ( $query->result() as $row ) {
            $order = $row;
        }

        return $order;
    }
    
    /**
     * Update data of order
     * 
     * @author  Dinov
     * @param   Int     $id     (Required)  Order ID
     * @param   Array   $data   (Required)  Array data of order
     * @return  Boolean Boolean false on failed process or invalid data, otherwise true
     */
    function update_data($id,$data){
        if(empty($id) || empty($data)) {return false;}
        $this->db->where('order_id',$id);	
        if($this->db->update($this->table_order,$data)) {return true;}
        else {return false;}
    }
}
/* End of file model_fundrising.php */
/* Location: ./application/models/model_fundrising.php */
